import { model, Model } from "./model";

export class Control {
    static instance: Control = new Control();
    
    model: Model;
    level_conf;
    toys_conf;

    constructor () {
        this.model = model;
        this.level_conf = this.model.getLevelConf();
        this.toys_conf = this.model.getToysConf();
    }
    
    getDisplayRows() {
        let tile_size = this.model.getTileSize()
        return Math.floor(cc.winSize.height / tile_size);
    }

    getCols() {
        return this.model.getCols();
    }

    getRows() {
        return this.model.getRows();
    }
    
    getTileSize() {
        return this.model.getTileSize();
    }

    getBlockNum(line: number, row: number) {
        if (!this.level_conf || !this.level_conf.length || !this.level_conf[line]) return 0;
        return this.level_conf[line][row];
    }

    getToyConf(idx: number) {
        let toy_conf = this.model.getToysConf()
        return toy_conf[idx % toy_conf.length];
    }

    getToyShape(type: number) {
        let toy_shape_conf = this.model.getToyShapeConf()
        return toy_shape_conf[type];
    }

    setBlockColor(block: cc.Node, block_num: number, cb?) {
        if (block_num && block_num !== 1) {
            cc.resources.load("textures/blocks/M" + block_num, cc.SpriteFrame, function (err, spriteFrame) {
                let spr = block.getChildByName("spr").getComponent(cc.Sprite);
                spr.spriteFrame = spriteFrame as cc.SpriteFrame;
                cb && cb(err, spriteFrame)
            });
        } else {
            cb && cb()
        }
    }
}

export const control = Control.instance;